package com.unicom.si.security.decide;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Iterator;

@Component("accessDecisionManager")
public class MyAccessDecisionManager implements AccessDecisionManager{

    /**
     * 该方法决定该权限是否有权限访问该资源，其实object就是一个资源的地址，authentication是当前用户的对应权限，
     * 如果没登陆就为游客，登陆了就是该用户对应的权限
     *
     * FilterSecurityInterceptor 和 AbstractSecurityInterceptor
     *
     * 参数说明：
     * authentication 用户的信息和所有用的权限，通过Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();获取用户所拥有的权限
     * object FilterInvocation对象，可以得到HttpServletRequest对象和response对象。可以通过object.toString()获得访问的URL地址
     * collection 这个URL地址所需要的权限，已经调用了MyFilterInvocationSecurityMetadataSource的 public Collection<ConfigAttribute> getAttributes(Object object)方法
     * 从requestMap中获取到了资源所需要的权限信息，然后在走到这个方法时传入进来Collection<ConfigAttribute>
     *
     * @param authentication
     * @param object
     * @param collection
     * @throws AccessDeniedException
     * @throws InsufficientAuthenticationException
     */
    public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> collection) throws AccessDeniedException, InsufficientAuthenticationException {

        if(null == collection){
            return;
        }

        //所请求的资源拥有的权限(一个资源对多个权限)
        Iterator<ConfigAttribute> iterable = collection.iterator();
        while (iterable.hasNext()){
            ConfigAttribute configAttribute = iterable.next();
            //访问所请求的资源所需要的权限
            String needPermission = configAttribute.getAttribute();
            System.out.println("访问"+object.toString()+"需要的权限是："+needPermission);

            //用户所拥有的权限authentication
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            for (GrantedAuthority ga : authorities) {
                if (needPermission.equals(ga.getAuthority())) {
                    return;
                }
            }
        }

        // 非匿名用户，走到这应该都是/** 能匹配上的，只要登陆成功了，都给放行
        if(!(authentication instanceof AnonymousAuthenticationToken)) {
            return;
        }

//        // 非匿名用户，并且要求的权限是authUser，表明只要是登录成功了，就可以放行，也就是登录成功的给与放行
//        if(!(authentication instanceof AnonymousAuthenticationToken)) {
//            if(iterable.hasNext()) {
//                ConfigAttribute configAttribute = iterable.next();
//                configAttribute.getAttribute().equals("authUser");
//                return;
//            }
//
//        }

        //没有权限
        throw new AccessDeniedException("没有权限访问！");
    }

    public boolean supports(ConfigAttribute configAttribute) {
        return true;
    }

    public boolean supports(Class<?> aClass) {
        return true;
    }
}
